package Model;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author thuydao
 */
import java.sql.*;
import java.util.ArrayList;
public class M_News {
        public M_News(){}
        private int matt;
        private String tieudett;
        private String mota;
        private Date ngaydang;
        private String hinhanh;
        
        private Connection conn;
        
        public M_News(int matt, String tieudett, String mota, Date ngaydang, String hinhanh){
            this.matt=matt;
            this.tieudett = tieudett;
            this.mota = mota;
            this.ngaydang =ngaydang;
            this.hinhanh = hinhanh;
            
        }

    
        
    
        
        public void connect(){
         try {
            String userName = "root"; // root là mặc định của MySQL.
            String passWord = "";
            String url = 
            "jdbc:mysql://localhost:3306/handmade?useUnicode=true&characterEncoding=UTF-8"; 
           
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            setConn(DriverManager.getConnection(url, userName, passWord));
            }
            catch (Exception e) {
                System.out.println("Diver sai");
            }
        
        }
    
    
    // Tạo Mã khách hàng
    
    
    // Thêm vào bảng khách hàng
    
    
    public M_News[] show_News(){
            connect();
            ResultSet rs=null;
            M_News[] result=null;
            try{
            String sql_show_News = "select *from tintuc"; 
           
            Statement st = getConn().createStatement();
            rs= st.executeQuery(sql_show_News);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int matt = rs.getInt("MaTT");
                String tieudett = rs.getString("TieuDeTT");
                String mota = rs.getString("MoTa");
                Date ngaydang = rs.getDate("NgayDang");
                String hinhanh = rs.getString("HinhAnh");
                
                M_News news = new M_News(matt, tieudett, mota, ngaydang, hinhanh);
                list.add(news);
                
                
            }
            result = new M_News[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            getConn().close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }
    
    public boolean insert_News(String tieudett, String mota, Date ngaydang, String hinhanh){
            connect();
            ResultSet rs = null;
            boolean result = false;
           
          try{
            String sql_insert_News = "insert into tintuc values("; 
          
            sql_insert_News += "'"+tieudett+"',"; 
            
         
            sql_insert_News += "'"+mota+"',";
            sql_insert_News += "'"+ngaydang+"',";
            sql_insert_News += "'"+hinhanh+"')";

            Statement st = getConn().createStatement();
             int val;         
             val = st.executeUpdate(sql_insert_News);

            result=true;
            }
            catch (SQLException s){
                System.out.println("SQL insert TAIKHOAN statement is not executed!"+ s.getMessage());
            }
          return result;
   }
    
    public boolean update_News(){
            connect();
            
            
           
          try{
            String sql_edit_KH = "update tintuc set MaTT=?, TieuDeTT=?, MoTa=?, NgayDang=?, HinhAnh=? where MaTT=?"; 
            PreparedStatement stm = getConn().prepareStatement(sql_edit_KH);
            stm.setInt(1, getMatt());
            stm.setString(2, getTieudett());
            stm.setString(3, getMota());
            stm.setDate(4, getNgaydang());
            stm.setString(5, getHinhanh());
           
            int val= stm.executeUpdate();
            stm.close();
            getConn().close();
           if(val>0)
                return true;
            
            }
            catch (SQLException s){
                System.out.println("QL insert TAIKHOAN statement is not executed!"+ s.getMessage());
            }
          return false;
   }
    
    public boolean delete_News(){
            connect();
            
            
           //DELETE FROM `danhmucsp` WHERE 1
          try{
            String sql_delete_News = "Delete from tintuc Where MaTT=?"; 
            
            PreparedStatement stm = getConn().prepareStatement(sql_delete_News);
            stm.setInt(1, getMatt());
            
            int val= stm.executeUpdate();
            stm.close();
            getConn().close();
            if(val>0)
                return true;
            
            
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
          return false;
   }
    
    public M_News[] search_News(String search){
            connect();
            ResultSet rs = null;
            M_News[] result=null;
            try{
            String sql_search_News = "select *from tintuc where MaTT like '%" + search + "or TieuDeTT like '%" + search + "%' or MoTa like '%" + search + "%' or NgayDang like '%" + search + "%'"; 
           
            Statement st = getConn().createStatement();
            rs= st.executeQuery(sql_search_News);
           
            ArrayList list = new ArrayList();
            while(rs.next()){
                int matt = rs.getInt("MaTT");
                String tieudett = rs.getString("TieuDeTT");
                String mota = rs.getString("MoTa");
                Date ngaydang = rs.getDate("NgayDang");
                String hinhanh = rs.getString("HinhAnh");
                
                M_News news = new M_News(matt, tieudett, mota, ngaydang, hinhanh);
                list.add(news);  
                
                
            }
            result = new M_News[list.size()];
            list.toArray(result);
            
            rs.close();
            st.close();
            getConn().close();
            return result;
            }
            catch (SQLException s){
                System.out.println(s.getMessage());
            }
            return null;
            
    }

    /**
     * @return the matt
     */
    public int getMatt() {
        return matt;
    }

    /**
     * @param matt the matt to set
     */
    public void setMatt(int matt) {
        this.matt = matt;
    }

    /**
     * @return the tieudett
     */
    public String getTieudett() {
        return tieudett;
    }

    /**
     * @param tieudett the tieudett to set
     */
    public void setTieudett(String tieudett) {
        this.tieudett = tieudett;
    }

    /**
     * @return the mota
     */
    public String getMota() {
        return mota;
    }

    /**
     * @param mota the mota to set
     */
    public void setMota(String mota) {
        this.mota = mota;
    }

    /**
     * @return the ngaydang
     */
    public Date getNgaydang() {
        return ngaydang;
    }

    /**
     * @param ngaydang the ngaydang to set
     */
    public void setNgaydang(Date ngaydang) {
        this.ngaydang = ngaydang;
    }

    /**
     * @return the hinhanh
     */
    public String getHinhanh() {
        return hinhanh;
    }

    /**
     * @param hinhanh the hinhanh to set
     */
    public void setHinhanh(String hinhanh) {
        this.hinhanh = hinhanh;
    }

    /**
     * @return the conn
     */
    public Connection getConn() {
        return conn;
    }

    /**
     * @param conn the conn to set
     */
    public void setConn(Connection conn) {
        this.conn = conn;
    }

    
    
    
}
